﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using FAS.Objetos;

namespace FAS
{
    public partial class SaveImage : Form
    {
        private RichTextBox richTextBox1;
        private String Name;
        [DllImport("USER32.dll")]
        private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);
        private const int WM_USER = 0x400;
        private const int EM_FORMATRANGE = WM_USER + 57;
        [StructLayout(LayoutKind.Sequential)]
        private struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct CHARRANGE
        {
            public int cpMin;
            public int cpMax;
        }

        public SaveImage(RichTextBox rc,String name) 
        {
            this.richTextBox1 = rc;
            this.Name = name;
            SaveAsJpegButton_Click(null,null);
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct FORMATRANGE
        {
            public IntPtr hdc;
            public IntPtr hdcTarget;
            public RECT rc;
            public RECT rcPage;
            public CHARRANGE chrg;
        }
        private const double inch = 14.4;
        private Rectangle contentRectangle;

        private void SaveAsJpegButton_Click(object sender, EventArgs e)
        {
            RtbToBitmap(richTextBox1, contentRectangle, Name);
        }
        private void RtbToBitmap(RichTextBox rtb, Rectangle rectangle, string fileName)
        {
            Bitmap bmp = new Bitmap(rtb.Width-10,rtb.Height-10);
            using (Graphics gr = Graphics.FromImage(bmp))
            {
                IntPtr hDC = gr.GetHdc();
                FORMATRANGE fmtRange;
                RECT rect;
                int fromAPI;
                rect.Top = 0; rect.Left = 0;
                rect.Bottom = (int)(bmp.Height + (bmp.Height * (bmp.HorizontalResolution / 100)) * inch) +20;
                rect.Right = (int)(bmp.Width + (bmp.Width * (bmp.VerticalResolution / 100)) * inch) +20;
                fmtRange.chrg.cpMin = 0;
                fmtRange.chrg.cpMax = -1;
                fmtRange.hdc = hDC;
                fmtRange.hdcTarget = hDC;
                fmtRange.rc = rect;
                fmtRange.rcPage = rect;
                int wParam = 1;
                IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
                Marshal.StructureToPtr(fmtRange, lParam, false);
                fromAPI = SendMessage(rtb.Handle, EM_FORMATRANGE, wParam, lParam);
                Marshal.FreeCoTaskMem(lParam);
                fromAPI = SendMessage(rtb.Handle, EM_FORMATRANGE, wParam, new IntPtr(0));
                gr.ReleaseHdc(hDC);
            }
            bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
            bmp.Dispose();
        }
        private void richTextBox1_ContentsResized(object sender, ContentsResizedEventArgs e)
        {
            contentRectangle = e.NewRectangle;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveAsJpegButton_Click(sender, e);
        }
    }
}
